import { apiUrl } from '../config'
import fecth from '../util/fetch'
import * as storage from '../config/storageTypes'
import { setCache } from '../util/storageUtils'
const modal = weex.requireModule('modal')

/**
 * 登录
 */
const doLogin = async (username, password) => {
  let parameters = {
    reqData: {
      username: username,
      password: password
    }
  }
  let res = await fecth.netFetch(apiUrl + 'login', 'POST', parameters, false)
  let data = res.data
  let flag = false
  if (data.retCode === 0) {
    flag = true
    const userinfo = res.data.dataitem
    await setCache(storage.CTCE_TOKEN, userinfo.key)
    await setCache(storage.CTCE_USER_BASIC, JSON.stringify(parameters.reqData))
    await setCache(storage.CTCE_DATALIST, res.data.datalist)
    await setCache(storage.CTCE_USER_INFO, JSON.stringify(userinfo))
  } else {
    flag = false
    modal.alert({
      message: data.retMessage
    })
  }
  return flag
}

export default {
  doLogin
}
